<?php


namespace app\common\middleware;


use app\home\model\Task;
use app\home\model\TaskRecord;
use traits\ResponsDataBuild;

class TaskCheck
{
    use ResponsDataBuild;

    public function handle($request, \Closure $next)
    {
        $nowTime = time();
        $param = $request->param();
        if (!empty($param['id'])) {
            //当前任务是否开始
            $task = Task::where('id', $param['id'])->field('startTime,endTime')->find();
            if ($nowTime < $task['startTime']) {
                return $this->returnError(2002);
            } elseif ($nowTime >= $task['startTime'] && $nowTime <= $task['endTime']) {
                return true;
            } else {
                //当前任务结束
                return $this->returnError(2003);
            }
            //当前任务是否被领取
            $taskCount = TaskRecord::where('task_id')->count();
            if (!empty($taskCount)) {
                return $this->returnError(2001);
            }
        }
        return $next($request);
    }
}